﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace iVision
{
    public class Marquee : ContentControl
    {
        bool IsPaused = false;
        bool IsLoaded;
        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();  

        public Marquee()
        {
            //DefaultStyleKey = typeof(Marquee);
           // RestartAnimation();
        }

        FrameworkElement cp;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            cp = this.GetTemplateChild("PART_Content") as FrameworkElement;
            Loaded += new RoutedEventHandler(Marquee_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(Marquee_SizeChanged);
        }

        void Marquee_SizeChanged(object sender, SizeChangedEventArgs e)
        {
             RestartAnimation();
        }

        void Marquee_Loaded(object sender, RoutedEventArgs e)
        {
            IsLoaded = true;
            
            this.MouseDown += new MouseButtonEventHandler(Marquee_MouseEnter);
            //this.MouseLeave += new MouseEventHandler(Marquee_MouseLeave);
            Animate();
        }

        void Marquee_MouseLeave(object sender, MouseEventArgs e)
        {
            sb.Resume();
        }

        void Marquee_MouseEnter(object sender, MouseButtonEventArgs e)
        {
            if (!IsPaused)
            {
                sb.Pause();
                IsPaused = true;
            }
            else
            {
                IsPaused = false;
                sb.Resume();
            }
        }

        public double Duration
        {
            get { return (double)GetValue(DurationProperty); }
            set { SetValue(DurationProperty, value); }
        }

        public static readonly DependencyProperty DurationProperty =
            DependencyProperty.Register("Duration", typeof(double), typeof(Marquee), new PropertyMetadata(ValueChanged));

        static void ValueChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            Marquee mar = sender as Marquee;
            mar.RestartAnimation();
        }

        private void RestartAnimation()
        {
            if (cp !=null && IsLoaded)
            {
                da.From = cp.ActualHeight;
                da.To = -cp.ActualHeight;
                da.RepeatBehavior = RepeatBehavior.Forever;
                da.Duration = new Duration( TimeSpan.FromSeconds( this.Duration));

                if (sb.Children.Count == 0)
                {
                    sb.Children.Add(da);
                    Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)"));
                    Storyboard.SetTarget(da, cp);
                    sb.Begin();
                }
            }
        }
        private void Animate() {
            if (cp != null && IsLoaded)
            { 
                da.From = cp.ActualHeight;
                da.To = -cp.ActualHeight;
                da.RepeatBehavior = RepeatBehavior.Forever;
                da.Duration = new Duration(TimeSpan.FromSeconds(this.Duration));
                Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)")); 
                sb.Children.Add(da); 
                sb.Begin(cp, true); 
            } 
        }
    

    }
}
